
<?php
defined('G_IN_SYSTEM')or exit('No permission resources.');
System::load_app_class('memberbase',null,'no');
System::load_app_fun('user','go');
System::load_app_fun('my','go');
System::load_sys_fun('send');
class user extends memberbase {
	public function __construct(){
		parent::__construct();
		$this->db = System::load_sys_class("model");
	}
	
	//退出登录
	public function cook_end(){
		_setcookie("uid","",time()-3600);
		_setcookie("ushell","",time()-3600);
//_message("退出成功",WEB_PATH."/mobile/mobile/");
		header("location: ".WEB_PATH."/mobile/mobile/");
	}
	
//返回登录页面
	public function login(){
		$webname=$this->_cfg['web_name'];
		$user = $this->userinfo;

		if($user){
			header("Location:".WEB_PATH."/mobile/home/");exit;
		}

		include templates("mobile/user","login");

	}
//微信登录
	public function wxlogin(){
		session_start();
		$this->db=System::load_sys_class('model');
		$wx_set =$this->db->GetOne("SELECT * from `@#_wxset` ");
		$state  = md5(uniqid(rand(), TRUE));
		$_SESSION["wxState"] = $state;
		$redirect_uri = urlencode("".$wx_set['mback']."/index.php/mobile/user/wx_callback/".$code."/");
		$wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$wx_set['mappid']."&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=$state#wechat_redirect";
		header("Location: $wxurl");
	}
	public function wx_callback(){
		session_start();
		$this->db=System::load_sys_class('model');
		$wx_set =$this->db->GetOne("SELECT * from `@#_wxset` ");
		$code = $_GET["code"];
		$response = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$wx_set['mappid']."&secret=".$wx_set['msecret']."&code=$code&grant_type=authorization_code");
		$jsondecode = json_decode($response,true);
		$wx_openid =$jsondecode["openid"];
		if(!$wx_openid){
			_messagemobile("绑定失败");
		}
		$access_token =$jsondecode["access_token"];
		$response= file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$wx_openid");
		$jsondecode = json_decode($response,true);
		$nickname = $jsondecode["nickname"];
		$nickname =mysql_real_escape_string($nickname);
		$unionid =$jsondecode["unionid"];
		$touimg = $jsondecode["headimgurl"];
		$go_user_info = $this->db->GetOne("select * from `@#_member_band` where `b_code` = '$wx_openid' and `b_type` = 'weixin' LIMIT 1");
		$decode=_encrypt($_COOKIE['regcode'],"DECODE");
		$decode = intval($decode);
		if(!$go_user_info){
			$userpass = md5("123456");
			$path = 'data/uploads/wximg/';
			$mulu = time();
			if($touimg){
			//$post_arr= array("img"=>$touimg,"path"=>$path,"mulu"=>$mulu);
				file_put_contents($path.$mulu.".jpg",file_get_contents($touimg));
			//$this->wximg($post_arr);
				_g_triggerRequest(WEB_PATH.'/mobile/user/wximg',false,$post_arr);
			}
			$go_user_img  = 'wximg/'.$mulu . '.jpg';
			$go_user_time = time();
			$q1 = $this->db->Query("INSERT INTO `@#_member` (`username`,`password`,`img`,`money`,`band`,`yaoqing`,`time`) VALUES ('$nickname','$userpass','$go_user_img',0,'weixin','$decode','$go_user_time')");
			$uid = $this->db->insert_id();
			$go_user_time = time();
			$this->db->Query("INSERT INTO `@#_member_band` (`b_uid`, `b_type`, `b_code`, `b_time`) VALUES ('$uid', 'weixin', '$wx_openid', '$go_user_time')");
			$member = $this->db->GetOne("select uid,password,mobile,email from `@#_member` where `uid` = '$uid' LIMIT 1");
			$se1 = _setcookie("uid",_encrypt($member['uid']),60*60*24*7);
			$se2 = _setcookie("ushell",_encrypt(md5($member['uid'].$member['password'].$member['mobile'].$member['email'])),60*60*24*7);
			$callback_url =  WEB_PATH."/mobile/home/mobilebind";
		//$callback_url =  WEB_PATH."/mobile/home/";
			header("Location:$callback_url");
		}else{
			$uid = $go_user_info["b_uid"];
			$member = $this->db->GetOne("select uid,password,mobile,email from `@#_member` where `uid` = '$uid' LIMIT 1");
			$se1 = _setcookie("uid",_encrypt($member['uid']),60*60*24*7);
			$se2 = _setcookie("ushell",_encrypt(md5($member['uid'].$member['password'].$member['mobile'].$member['email'])),60*60*24*7);
			if(!$member['mobile']){
		  	// _message("登录成功，请绑定邮箱或手机号和及时修改默认密码!",$callback_url);
				$callback_url =  WEB_PATH."/mobile/home/mobilebind";
		//$callback_url =  WEB_PATH."/mobile/home/";
				header("Location:$callback_url");
			}else{
				$uid = $go_user_info["b_uid"];
				$member = $this->db->GetOne("select uid,password,mobile,email from `@#_member` where `uid` = '$uid' LIMIT 1");
				if(empty($member)){
					$this->db->Query("delete from `@#_member_band` where `b_id` = '".$go_user_info['b_id']."'");
					$userpass = md5('123456');
					$path = 'data/uploads/wximg/';
					$mulu = time();
					if($touimg){
						$post_arr= array("img"=>$touimg,"path"=>$path,"mulu"=>$mulu);
						file_put_contents($path.$mulu.".jpg",file_get_contents($touimg));
						_g_triggerRequest(WEB_PATH.'/mobile/user/wximg',false,$post_arr);
					}
					$go_user_img  = 'wximg/'.$mulu . '.jpg';
					$go_user_time = time();
					$q1 = $this->db->Query("INSERT INTO `@#_member` (`username`,`password`,`img`,`money`,`band`,`yaoqing`,`time`) VALUES ('$nickname','$userpass','$go_user_img',0,'weixin','$decode','$go_user_time')");
					$uid = $this->db->insert_id();
					$this->db->Query("INSERT INTO `@#_member_band` (`b_uid`, `b_type`, `b_code`, `b_time`) VALUES ('$uid', 'weixin', '$wx_openid', '$go_user_time')");
					$member = $this->db->GetOne("select uid,password,mobile,email from `@#_member` where `uid` = '$uid' LIMIT 1");
					$se1 = _setcookie("uid",_encrypt($member['uid']),60*60*24*7);
					$se2 = _setcookie("ushell",_encrypt(md5($member['uid'].$member['password'].$member['mobile'].$member['email'])),60*60*24*7);
					$callback_url =  WEB_PATH."/mobile/home/mobilebind";
			//$callback_url =  WEB_PATH."/mobile/home/";
					header("Location:$callback_url");
				}
				$se1 = _setcookie("uid",_encrypt($member['uid']),60*60*24*7);
				$se2 = _setcookie("ushell",_encrypt(md5($member['uid'].$member['password'].$member['mobile'].$member['email'])),60*60*24*7);
				$callback_url =  WEB_PATH."/mobile/home/";
				header("Location:$callback_url");
			}
		}
	}

	public function wximg(){
		$imgurl = trim($_POST['img']);
		$path = trim($_POST['path']);
		$mulu = trim($_POST['mulu']);
		$IMG = file_get_contents($imgurl);
		file_put_contents($path ."/". $mulu . '.jpg',$IMG);
		file_put_contents($path ."/". $mulu . '.jpg_160160.jpg',$IMG);
		file_put_contents($path ."/". $mulu . '.jpg_3030.jpg',$IMG);
		file_put_contents($path ."/". $mulu . '.jpg_8080.jpg',$IMG);
		echo 1;
	}
//用户登录
	public function userlogins(){
		$phone = trim($_POST['phone']);
		$pwd = base64_decode(trim($_POST['password']));
		$password=md5(safe_replace($pwd));

		$member=$this->db->GetOne("select * from `@#_member` where `mobile`='$phone'");
		if ($member) {
			if($member['mobilecode']==1 || $member1['emailcode'] == 1){
				$member1=$this->db->GetOne("select * from `@#_member` where `mobile`='$phone' and `password`='$password'");
				if ($member1) {
					echo json_encode(array('sta' => '1', 'msg' => '登录成功！'));
					_setcookie("uid",_encrypt($member['uid']),60*60*24*7);
					_setcookie("ushell",_encrypt(md5($member['uid'].$member['password'].$member['mobile'].$member['email'])),60*60*24*7);
					die;
				}else{
					echo json_encode(array('sta' => '0', 'msg' => '用户名，密码错误！'));
					die;
				}
			}
		}else{
			echo json_encode(array('sta' => '0', 'msg' => '该用户未注册！'));
			die;
		}

	}
//返回注册页面
	public function register(){
		$webname=$this->_cfg['web_name'];
		$webtitle="登录/注册";

		include templates("mobile/user","login");
	}

//返回发送验证码页面
	public function mobilecode(){
		$webname=$this->_cfg['web_name'];
		$mobilename=$this->segment(4);

		include templates("mobile/user","mobilecheck");
	}

	public function mobilecheck(){
		$webname=$this->_cfg['web_name'];
		$title="验证手机";
		$time=3000;
		$name=$this->segment(4);
		$member=$this->db->GetOne("SELECT * FROM `@#_member` WHERE `mobile` = '$name' LIMIT 1");
//var_dump($member);exit;
		if(!$member)_message("参数不正确!");
		if($member['mobilecode']==1){
			_message("该账号验证成功",WEB_PATH."/mobile/mobile");
		}
		if($member['mobilecode']==-1){
			$sendok = send_mobile_reg_code($name,$member['uid']);
			if($sendok[0]!=1){
				_message($sendok[1]);
			}
			header("location:".WEB_PATH."/mobile/user/mobilecheck/".$member['mobile']);
			exit;
		}


		$enname=substr($name,0,3).'****'.substr($name,7,10);
		$time=120;
		include templates("mobile/user","mobilecheck");
	}
	//发送注册验证码
	public function adduser(){
		$webname=$this->_cfg['web_name'];
		$phone=trim($_POST['phone']);
		$member=$this->db->GetOne("SELECT * FROM `@#_member` WHERE `mobile` = '$phone' LIMIT 1");

		if (is_array($member)){
			if ($member[mobilecode] == 1 || $member[emailcode] == 1) {
				echo json_encode(array('sta' => '0', 'msg' => '该用户已经注册！'));
				exit;
			}else{
				if($member['mobilecode'] !=-1){

					$sendok=send_mobile_reg_code($phone,$member['uid']);

					if($sendok[0]!=1){
						echo json_encode(array('sta'=>'0','msg'=>'验证码发送失败！'));
						die;
					}else{
						echo json_encode(array('sta'=>'1','msg'=>'验证码发送成功！'));
					}
				}
			}
		}else{
			$time = time();
			$ip = _get_ip();
			$this->db->Query("INSERT INTO `@#_member`(username,mobile,reg_key,user_ip,img,emailcode,phonecode,time)
				VALUES('$phone','$phone','$phone','$ip','photo/member.jpg','-1','-1','$time')");
			$member1=$this->db->GetOne("SELECT * FROM `@#_member` WHERE `mobile` = '$phone' LIMIT 1");
			
			$sendok=send_mobile_reg_code($phone,$member1['uid']);;
			if($sendok[0]!=1){
				echo json_encode(array('sta'=>'0','msg'=>'验证码发送失败！'));
				die;
			}else{
				echo json_encode(array('sta'=>'1','msg'=>'验证码发送成功！'));            
				die;
			}

		}
	}
 //注册验证码验证
	public  function  updateusers(){

		$phone= trim($_POST['phone']);
		$randcode = trim($_POST['randcode']);
		$password = trim($_POST['password']);
		$password=md5($password);
		$member=$this->db->GetOne("SELECT * FROM `@#_member` WHERE `mobile`='$phone' LIMIT 1");
		$uid = $member['uid'];
		if ($member) {
			$usercode=explode("|",$member['mobilecode']);
			if($randcode == $usercode[0]){
				$this->db->Query("UPDATE `@#_member` SET `mobilecode`='1',`password`='$password'  where `uid`='$uid'");
				echo json_encode(array('sta'=>'1','msg'=>"验证成功，请登录！"));
				die;
			}else{
				echo json_encode(array('sta'=>'0','msg'=>'验证码错误！'));
				die;
				
			}
		}else{
			echo json_encode(array('sta'=>'0','msg'=>'验证码错误！'));
			die;
		}


	}
//订单详情
	public function buyDetail(){
		$member=$this->userinfo;
		$itemid=intval($this->segment(4));
		$webtitle="订单详情";
		$itemlist=$this->db->GetList("select *,a.time as timego,sum(gonumber) as gonumber from `@#_member_go_record` a left join `@#_shoplist` b on a.shopid=b.id where a.uid='$member[uid]' and b.id='$itemid' group by a.id order by a.time" );
		$getuid = $itemlist[0]['q_uid'];
		$getname=$this->db->GetOne("select * from `@#_member` where uid='$getuid'  " );
		if(!empty($itemlist)){
			if($itemlist[0]['q_end_time']!=''){
//商品已揭晓
				$itemlist[0]['codeState']='已揭晓...';
				$itemlist[0]['class']='z-ImgbgC02';
			}elseif($itemlist[0]['shenyurenshu']==0){
//商品购买次数已满
				$itemlist[0]['codeState']='已满员...';
				$itemlist[0]['class']='z-ImgbgC01';
			}else{
//进行中
				$itemlist[0]['codeState']='进行中...';
				$itemlist[0]['class']='z-ImgbgC01';

			}
			$bl=($itemlist[0]['canyurenshu']/$itemlist[0]['zongrenshu'])*100;

			foreach ($itemlist as $k => $v) {
				$count += $v['gonumber'];
			}
		}
		$count = $count ? $count : 0;

		include templates("mobile/user","userbuyDetail");
	}

}

?>